﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YJKBatchPrinter
{
	public partial class viewSheetSetForm : System.Windows.Forms.Form
	{
		public viewSheetSetForm(ViewSheets viewSheets)
		{
			this.InitializeComponent();
			this.m_viewSheets = viewSheets;
		}

		private void ViewSheetSetForm_Load(object sender, EventArgs e)
		{
			this.c.DataSource = this.m_viewSheets.ViewSheetSetNames;
			this.c.SelectedValueChanged += this.viewSheetSetNameComboBox_SelectedValueChanged;
			this.c.SelectedItem = this.m_viewSheets.SettingName;
			this.showSheetsCheckBox.Checked = true;
			this.showViewsCheckBox.Checked = true;
			this.ListViewSheetSet();
			this.viewSheetSetListView.ItemChecked += this.viewSheetSetListView_ItemChecked;
		}

		private void ListViewSheetSet()
		{
			VisibleType visibleType;
			if (this.showSheetsCheckBox.Checked && this.showViewsCheckBox.Checked)
			{
				visibleType = VisibleType.VT_BothViewAndSheet;
			}
			else if (this.showSheetsCheckBox.Checked && !this.showViewsCheckBox.Checked)
			{
				visibleType = VisibleType.VT_SheetOnly;
			}
			else if (!this.showSheetsCheckBox.Checked && this.showViewsCheckBox.Checked)
			{
				visibleType = VisibleType.VT_ViewOnly;
			}
			else
			{
				visibleType = VisibleType.VT_None;
			}
			List<Autodesk.Revit.DB.View> list = this.m_viewSheets.AvailableViewSheetSet(visibleType);
			this.viewSheetSetListView.Items.Clear();
			foreach (Autodesk.Revit.DB.View view in list)
			{
				ListViewItem listViewItem = new ListViewItem(RevitViewExtension.GetViewTitle(view));
				listViewItem.Checked = this.m_viewSheets.IsSelected(listViewItem.Text);
				this.viewSheetSetListView.Items.Add(listViewItem);
			}
		}

		private void viewSheetSetNameComboBox_SelectedValueChanged(object sender, EventArgs e)
		{
			if (this.m_stopUpdateFlag)
			{
				return;
			}
			this.m_viewSheets.SettingName = (this.c.SelectedItem as string);
			this.ListViewSheetSet();
			this.saveButton.Enabled = (this.revertButton.Enabled = false);
			this.reNameButton.Enabled = (this.deleteButton.Enabled = (this.m_viewSheets.SettingName.Equals("<在任务中>") ? false : true));
		}

		private void showSheetsCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this.ListViewSheetSet();
		}

		private void showViewsCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this.ListViewSheetSet();
		}

		private void saveButton_Click(object sender, EventArgs e)
		{
			List<string> list = new List<string>();
			foreach (object obj in this.viewSheetSetListView.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Checked)
				{
					list.Add(listViewItem.Text);
				}
			}
			this.m_viewSheets.ChangeCurrentViewSheetSet(list);
		}

		private void saveAsButton_Click(object sender, EventArgs e)
		{
			using (SaveAsForm saveAsForm = new SaveAsForm(this.m_viewSheets))
			{
				saveAsForm.ShowDialog();
				if (DialogResult.OK == saveAsForm.DialogResult)
				{
					this.chgCurPrintViewSheetSet();
				}
			}
			this.m_stopUpdateFlag = true;
			this.c.DataSource = this.m_viewSheets.ViewSheetSetNames;
			this.c.Update();
			this.m_stopUpdateFlag = false;
			this.c.SelectedItem = this.m_viewSheets.SettingName;
		}

		private void revertButton_Click(object sender, EventArgs e)
		{
			this.m_viewSheets.Revert();
			this.ViewSheetSetForm_Load(null, null);
		}

		private void reNameButton_Click(object sender, EventArgs e)
		{
			using (ReNameForm reNameForm = new ReNameForm(this.m_viewSheets))
			{
				reNameForm.ShowDialog();
			}
			this.m_stopUpdateFlag = true;
			this.c.DataSource = this.m_viewSheets.ViewSheetSetNames;
			this.c.Update();
			this.m_stopUpdateFlag = false;
			this.c.SelectedItem = this.m_viewSheets.SettingName;
		}

		private void deleteButton_Click(object sender, EventArgs e)
		{
			this.m_viewSheets.Delete();
			this.m_stopUpdateFlag = true;
			this.c.DataSource = this.m_viewSheets.ViewSheetSetNames;
			this.c.Update();
			this.m_stopUpdateFlag = false;
			this.c.SelectedItem = this.m_viewSheets.SettingName;
		}

		private void checkAllButton_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.viewSheetSetListView.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				listViewItem.Checked = true;
			}
		}

		private void checkNoneButton_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.viewSheetSetListView.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				listViewItem.Checked = false;
			}
		}

		private void viewSheetSetListView_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			if (!this.m_viewSheets.SettingName.Equals("<在任务中>") && !this.saveButton.Enabled)
			{
				this.saveButton.Enabled = (this.revertButton.Enabled = (this.reNameButton.Enabled = true));
			}
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			if (this.c.Text.Equals("<在任务中>"))
			{
                TaskDialogResult taskDialogResult = TaskDialog.Show("保存设置", "是否保存当前的设置以供Revit使用", (TaskDialogCommonButtons)6);
                if (7 == (int)taskDialogResult)
				{
					this.chgCurPrintViewSheetSet();
					return;
				}
                if (6 == (int)taskDialogResult)
				{
					using (SaveAsForm saveAsForm = new SaveAsForm(this.m_viewSheets))
					{
						saveAsForm.ShowDialog();
						if (DialogResult.OK == saveAsForm.DialogResult)
						{
							this.chgCurPrintViewSheetSet();
						}
					}
					this.m_stopUpdateFlag = true;
					this.c.DataSource = this.m_viewSheets.ViewSheetSetNames;
					this.c.Update();
					this.m_stopUpdateFlag = false;
					this.c.SelectedItem = this.m_viewSheets.SettingName;
				}
			}
		}

		private void chgCurPrintViewSheetSet()
		{
			List<string> names = (List<string>)this.getListSelItemsNames(this.viewSheetSetListView);
			this.m_viewSheets.ChangeCurrentViewSheetSet(names);
		}

		private IList<string> getListSelItemsNames(ListView list)
		{
			IList<string> list2 = new List<string>();
			if (list != null)
			{
				foreach (object obj in list.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					if (listViewItem.Checked)
					{
						list2.Add(listViewItem.Text);
					}
				}
			}
			return list2;
		}

		private void helpButton_Click(object sender, EventArgs e)
		{
			string sWeb = "http://help.autodesk.com/view/RVT/2015/CHS/?guid=GUID-C9D5E601-C402-41A4-8521-4233EC8B03D3";
			PrinterFilter.openWebWithdefaultScanning(sWeb);
		}

		private ViewSheets m_viewSheets;

		private bool m_stopUpdateFlag;

		public class ListViewEx : ListView
		{
			[DllImport("user32.dll")]
			public static extern int ShowScrollBar(IntPtr hWnd, int iBar, int bShow);

			protected override void WndProc(ref Message m)
			{
                if (base.View == System.Windows.Forms.View.List)
				{
					viewSheetSetForm.ListViewEx.ShowScrollBar(base.Handle, 1, 1);
					viewSheetSetForm.ListViewEx.ShowScrollBar(base.Handle, 0, 0);
				}
				base.WndProc(ref m);
			}

			private const int SB_HORZ = 0;

			private const int SB_VERT = 1;
		}
	}
}
